package test.asm;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.EmptyVisitor;
import org.objectweb.asm.tree.ClassNode;

public class ASM {
	private static Logger log = Logger.getLogger(ASM.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String file = "E:\\Projects\\Java\\Eclipse\\learning\\my\\dependencies\\bin\\test\\Hello.class";
		InputStream is = new FileInputStream(file);

		ClassReader cr = new ClassReader(is);
		ClassNode root = new ClassNode();
		cr.accept(root, ClassReader.SKIP_DEBUG);
		root.accept(new PrintClass());
	}

	static class PrintClass extends EmptyVisitor {
		@Override
		public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
			log.info(("access=" + access + ", name=" + name + ", desc=" + desc + ", sign=" + signature));
			return new PrintMethod();
		}
	}

	static class PrintMethod implements MethodVisitor {

		@Override
		public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public AnnotationVisitor visitAnnotationDefault() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void visitAttribute(Attribute attr) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitCode() {
			log.info("---start visit method---");
		}

		@Override
		public void visitEnd() {
			log.info("---end visit method---");
		}

		@Override
		public void visitFieldInsn(int opcode, String owner, String name, String desc) {
			log.info(("owner=" + owner + ", name=" + name + ", desc=" + desc));
		}

		@Override
		public void visitFrame(int type, int local, Object[] local2, int stack, Object[] stack2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitIincInsn(int var, int increment) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitInsn(int opcode) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitIntInsn(int opcode, int operand) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitJumpInsn(int opcode, Label label) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitLabel(Label label) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitLdcInsn(Object cst) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitLineNumber(int line, Label start) {
			log.info(("line=" + line + ", start=" + start));
		}

		@Override
		public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
			log.info(("sign=" + signature + ", name=" + name + ", desc=" + desc + ", start=" + start + ", end=" + end));
		}

		@Override
		public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitMaxs(int maxStack, int maxLocals) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitMethodInsn(int opcode, String owner, String name, String desc) {
			log.info(("owner=" + owner + ", name=" + name + ", desc=" + desc));
		}

		@Override
		public void visitMultiANewArrayInsn(String desc, int dims) {
			// TODO Auto-generated method stub

		}

		@Override
		public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitTypeInsn(int opcode, String type) {
			// TODO Auto-generated method stub

		}

		@Override
		public void visitVarInsn(int opcode, int var) {
			log.info("opcode=" + opcode + ", var=" + var);
		}

	}
}
